<?php
declare(strict_types=1);

namespace App\Common\Modular\System\Annotation;

use App\Common\Modular\System\Serve\SeoServe;
use Attribute;
use Zms\Unit\Str;
use Hyperf\Di\Annotation\AbstractAnnotation;
use Hyperf\Di\Annotation\AnnotationCollector;

#[Attribute(Attribute::TARGET_METHOD)]
class Seo extends AbstractAnnotation
{
    /**
     * 所属应用
     * @var string
     */
    public string $app;


    /**
     * 动态页面
     * @var bool
     */
    public bool $dynamic;


    /**
     * 页面名称
     * @var string
     */
    public string $name;


    /**
     * 可用参数
     * @var array
     */
    public array $param = [];


    /**
     * 是否禁用规则
     * @var bool
     */
    public bool $disable;

    /**
     * Seo标识
     * @var string
     */
    public string $sign;


    /**
     * Seo回调
     * @var array
     */
    public array $callback = [];


    /**
     * 页面静态化,防止恶意耍页面,cache配置参数时按参数来缓存页面
     * 如 ['id','name','uid']  则缓存页面按所配置的参数来缓存,不配置不缓存
     * @var array
     */
    public array $cache = [];

    /**
     * @param string $name
     * @param array $param
     * @param bool $disable
     * @param string $app
     * @param array $cache
     */
    public function __construct(
        string $name,
        array  $param = [],
        bool   $disable = false,
        string $app = SeoServe::DEFAULT_APP,
        array  $cache = [],
    )
    {
        $this->name = $name;
        $this->param = $param;
        $this->dynamic = count($param) > 0;
        $this->disable = $disable;
        $this->app = $app;
        $this->cache = $cache;
    }


    public function collectMethod(string $className, ?string $target): void
    {
        $this->sign = Str::md5_16($className . '::' . $target);
        $this->callback = [$className, $target];
        AnnotationCollector::collectMethod($className, $target, static::class, $this);
    }

}